package sw6.visualschedule;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Pair;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;


public class HorizontalScroll extends Activity {
  
	/** Called when the activity is first created. */
	
	LinearLayout mLinView;
	TextView mTexView;
	
	Handler handler;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.horizontal);
        
        this.mLinView = (LinearLayout) findViewById(R.id.horizontalLinearLayout);
        
        PictureList picList = new PictureList(
        		new ArrayList<Integer>(),
        		new ArrayList<String>()
        );

        picList.add(R.drawable.wakeup_l, "Godmorgen");
        picList.add(R.drawable.sitontoilet_l, "Gå på toilettet");
        picList.add(R.drawable.shower_l, "Vask dig");
        picList.add(R.drawable.putonclothes_l, "Tag tøj på");
        picList.add(R.drawable.takemedicinepills_l, "Medicin");
        picList.add(R.drawable.eat_l, "Spis morgenmad");
        picList.add(R.drawable.playground_l, "Ud og leg, far og mor vil være alene");
        picList.add(R.drawable.movietheater_l, "Vi skal I biografen");
        picList.add(R.drawable.sleep_l, "Godnat");
        
        for (Pair<Integer, String> pic : picList) {
        	ImageButton ib = new ImageButton(this);	
        	
        	Display display = getWindowManager().getDefaultDisplay(); 
        	int width = display.getWidth();
        	int height = display.getHeight();        	
        	
        	this.mTexView = new TextView(this);
        	mTexView.setWidth((int) (Math.random() * width));
        	mLinView.addView(mTexView);        
        	
        	ib.setImageResource(pic.first);
        	ib.setAdjustViewBounds(true);
        	ib.setMaxHeight((height/3));
        	
        	ib.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					
					HorizontalScroll.this.startActivity(new Intent(HorizontalScroll.this, VerticalScroll.class));
					mLinView.removeView(v);
					mLinView.removeViewAt(0);
					/*
					ImageButton imageView = ((ImageButton)v);
					ColorMatrix cm = new ColorMatrix();
					cm.setSaturation(0);
					ColorMatrixColorFilter filter = new ColorMatrixColorFilter(cm);
					imageView.setColorFilter(filter);
					*/
				}
			});
        	mLinView.addView(ib);
        	/*
        	if(pic.second != null) //add text
        	{
        		TextView tv = new TextView(this);
        		tv.setText(pic.second);
        		tv.setGravity(Gravity.CENTER);
        		linView.addView(tv);
        	
        	}
        	*/
        	
		}
        
        /*
        //Create button event listener
        final Button button = (Button) findViewById(R.id.Button01);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {            	
            	HorizontalScrollView hscroll = (HorizontalScrollView)findViewById(R.id.HorizontalScrollBar);
                hscroll.scrollBy(500, 200);  
            }
        });
        
        final Button button2 = (Button) findViewById(R.id.Button02);
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	HorizontalScroll.this.startActivity(new Intent(HorizontalScroll.this, VerticalScroll.class));  
            }
        });
        
        HorizontalScrollView hscroll = (HorizontalScrollView)findViewById(R.id.HorizontalScrollBar);
        */
                
    }
    
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
    	//Called when the current Window of the activity gains or loses focus. This is the best indicator of whether this activity is visible to the user.
    	//http://www.seangri-la.com/cgi-bin/moin.cgi/onResume_!%3D_Visible_to_user
    	super.onWindowFocusChanged(hasFocus);
    	if (hasFocus) {
    		
    		HorizontalScrollView hscroll = (HorizontalScrollView)findViewById(R.id.HorizontalScrollBar);
        	hscroll.scrollTo(0, 0);        	
    	}
    }
    
    @Override
    public boolean onKeyDown(int keycode, KeyEvent event) {
        if (keycode == KeyEvent.KEYCODE_MENU) {
            HorizontalScroll.this.startActivity(new Intent(HorizontalScroll.this, ActivitiesOfTheDay.class));
        }
        
        if (keycode == KeyEvent.KEYCODE_BACK) {
        	HorizontalScrollView hscroll = (HorizontalScrollView)findViewById(R.id.HorizontalScrollBar);
        	hscroll.scrollTo(0, 0);
        	return true;
        }
        
        return super.onKeyDown(keycode,event);
    }
}

